<?php
require_once("Panel/ThemeModel.php");
require_once("Panel/ThemeView.php");

class ThemeController {
	private $_parameters;
	private $header_photo;
	private $content_wrap;
	private $body_bg;
	
	function __construct($__parameters=null){		
		global $config;
		$this->_parameters = $__parameters;
		$sub = str_replace('www.','',$_SERVER['HTTP_HOST']); 
		$this->header_photo  = "../public/users/".$sub."/header-photo.jpg";
		$this->content_wrap  = "../public/users/".$sub."/content-wrap.jpg";
		$this->body_bg  = "../public/users/".$sub."/body-bg.jpg";
	}
	

	
	function indexAction($__parameters=null){
		global $dispatcher;
		
		// kturą stronę wczytać
		$__page = theme::_define_page();
		// wczytaj dane strony: descriptions, kewords, nazwa
		$dispatcher->handleEvent("Panel_Panel,showpage,{$__page}");
		
		// które sekcje posiada wybrana strone
		$__section_info = theme::get_section_info($__page);
		// jaki układ/wygląd posiada wybrana strona
		$__theme_info = theme::get_theme_info($__page);
		// wczytaj sekcje wybranej strony
		view_theme::__show_section_text($__section_info,$__theme_info,$__page);
		// pokaż stronę
		view_theme::__display_page($__theme_info);
	}

	public function editthemeAction(){
		global $objSession;
	if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		view_theme::showRamki();
	}
	
	public function saveramkiAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		$this->uploadfilesAction();
		theme::saveSettings();
	}
	
	public function resetujUstawieniaAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		theme::resetujUstawienia();
		tools::_redirect('zarzadzaj_wygladem.html',null,0,'php');
	}
	
	public function del_main_plik_tlaAction(){
		tools::_delete_file($this->content_wrap);
	}
	
	public function del_main_plik_tla_tresciAction(){
		tools::_delete_file($this->body_bg);
	}
	
	public function del_baner_fileAction(){
		tools::_delete_file($this->header_photo);
	}
	
	private function uploadfilesAction(){
		$max_wielkosc_pliku = 1048576 * 1; // 1MB = 1048576;
		foreach ($_FILES as $fieldName => $file) {
			switch($fieldName){
				case 'baner_file': // baner górny
					if ($file['size'] <= $max_wielkosc_pliku && $file['error'] == 0 ) {
						$up=move_uploaded_file($file['tmp_name'],  $this->header_photo );
					}else{
						$up = false;
					}
					break;
				case 'main_plik_tla': // plik tła
					if ($file['size'] <= $max_wielkosc_pliku && $file['error'] == 0 ) {
						$up=move_uploaded_file($file['tmp_name'],  $this->content_wrap );
					}else{
						$up = false;
					}
					break;	
				case 'main_plik_tla_tresci': // plik tła
					if ($file['size'] <= $max_wielkosc_pliku && $file['error'] == 0 ) {
						$up=move_uploaded_file($file['tmp_name'],  $this->body_bg );
					}else{
						$up = false;
					}
					break;			
			}
		}
		return true;		
	}
}

?>